package com.song.flowable.listener;

import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class FlowableGlobalListener implements ApplicationListener<ContextRefreshedEvent> {
    private final SpringProcessEngineConfiguration configuration;

    private final TaskCreateListener taskCreateListener;

    private final TaskEndListener taskEndListener;

    private final ProcessEndListener processEndListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        // 任务创建全局监听-待办消息提醒
        dispatcher.addEventListener(taskCreateListener, FlowableEngineEventType.TASK_CREATED);
        //待办任务完成
        dispatcher.addEventListener(taskEndListener, FlowableEngineEventType.TASK_COMPLETED);
        //流程结束调用
        dispatcher.addEventListener(processEndListener,FlowableEngineEventType.PROCESS_COMPLETED);
    }
}
